<template>
    <el-dialog v-model="dialogVisible" :title="!addForm.dictCode ? '新增' : '编辑'">
        <xkl-form :form="addForm"></xkl-form>
        <template #footer>
            <el-button @click="close()">取消</el-button>
            <el-button type="primary" @click="confirm()">确认</el-button>
        </template>
    </el-dialog>
</template>
<script setup lang="ts">
export interface DialogOperateInstance {
    init: (id?: number) => void
}
import { nextTick, ref } from 'vue';
import useQuery from './hooks/query';
import { addForm } from './hooks/_form_table';
import useUpdate from './hooks/update';

const { getDictDataById, getDataList } = useQuery()
const { submit } = useUpdate()

const dialogVisible = ref(false)

const init = (id?: number) => {
    dialogVisible.value = true
    nextTick(() => {
        addForm.dictCode = 0
        addForm.clear()
        addForm.dictType = $route.query.type as string
        if (id) {
            addForm.dictCode = id
            getDictDataById(addForm.dictCode)
        }
    })
}

const confirm = () => {
    addForm.submit(async (done) => {
        await submit()
        getDataList()
        close()
        done()
    })
}

const close = () => {
    dialogVisible.value = false
}



defineExpose({ init })
</script>
<style lang="scss" scoped></style>